package com.kongchengji.zhanguafou.utils;

import com.alibaba.fastjson.JSONObject;
import com.kongchengji.zhanguafou.domain.StationInfoDO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import tk.mybatis.mapper.entity.Example;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class GetStationUtil {
    static Logger logger = LoggerFactory.getLogger(GetStationUtil.class);

    public static StationInfoDO get(String url) {
        BufferedReader br = null;
        try {
            String[] cmds = {"curl", "-o", "/dev/null", "-L", "-w", "{code:\"%{http_code}\",parseTime:\"%{time_namelookup}\","
                    + "connectTime:\"%{time_connect}\","
                    + "downLoadTime:\"%{time_total}\"}", url};
            ProcessBuilder pb = new ProcessBuilder(cmds);
            pb.redirectErrorStream(true);
            Process p;
            p = pb.start();
            String line;
            br = new BufferedReader(new InputStreamReader(p.getInputStream(), "gbk"));
            String result = null;
            while ((line = br.readLine()) != null) {
                result = line;
            }
            result = result.substring(result.indexOf("{"));
            //将json格式的结果转为对象
            StationInfoDO stationInfoDO = JSONObject.parseObject(result, StationInfoDO.class);
            return stationInfoDO;
        } catch (IOException e) {
            return null;
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
